<html>
<head><meta charset="utf-8"><title>Using rustc_ap_rustc_parse::parse_crate_from_file · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html">Using rustc_ap_rustc_parse::parse_crate_from_file</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="196712429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196712429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dzmitry Malyshau <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196712429">(May 06 2020 at 23:55)</a>:</h4>
<p>I'm trying to figure out how this function works, and I'm missing something. If we pass a "<a href="http://lib.rs" title="http://lib.rs">lib.rs</a>" of a valid library crate, I'd expect it to contain all the modules. However, running it on a simple project that has <code>crate::submodule::subsubmodule</code> path, it appears that the reflected crate doesn't contain it:</p>
<div class="codehilite"><pre><span></span><code>crate
    A
    submodule
    tests
</code></pre></div>


<p>I'm printing this with the following code:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">print_module</span><span class="p">(</span><span class="n">indent</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="n">name</span>: <span class="kp">&amp;</span><span class="kt">str</span><span class="p">,</span><span class="w"> </span><span class="n">module</span>: <span class="kp">&amp;</span><span class="nc">rustc_ast</span>::<span class="n">ast</span>::<span class="n">Mod</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">..</span><span class="w"> </span><span class="n">indent</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">print</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\t</span><span class="s">&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">module</span><span class="p">.</span><span class="n">items</span><span class="p">.</span><span class="n">iter</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">ItemKind</span>::<span class="n">Mod</span><span class="p">(</span><span class="k">ref</span><span class="w"> </span><span class="n">child</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">item</span><span class="p">.</span><span class="n">kind</span><span class="w">  </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">item</span><span class="p">.</span><span class="n">ident</span><span class="p">.</span><span class="n">to_string</span><span class="p">();</span><span class="w"></span>
<span class="w">            </span><span class="n">print_module</span><span class="p">(</span><span class="n">indent</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="n">child</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="196712516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196712516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dzmitry Malyshau <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196712516">(May 06 2020 at 23:56)</a>:</h4>
<p><a href="/user_uploads/4715/oY8vCmYcGXwSExmjnjKtJQzr/Screen-Shot-2020-05-06-at-19.53.12.png" title="Screen-Shot-2020-05-06-at-19.53.12.png">Screen-Shot-2020-05-06-at-19.53.12.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/oY8vCmYcGXwSExmjnjKtJQzr/Screen-Shot-2020-05-06-at-19.53.12.png" title="Screen-Shot-2020-05-06-at-19.53.12.png"><img src="/user_uploads/4715/oY8vCmYcGXwSExmjnjKtJQzr/Screen-Shot-2020-05-06-at-19.53.12.png"></a></div>



<a name="196712569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196712569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dzmitry Malyshau <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196712569">(May 06 2020 at 23:56)</a>:</h4>
<p>The <code>submodule/mod.rs</code> clearly contains the "subsubmodule":</p>
<div class="codehilite"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">mod</span> <span class="nn">subsubmodule</span><span class="p">;</span><span class="w"></span>

<span class="k">use</span><span class="w"> </span><span class="k">crate</span>::<span class="n">A</span><span class="p">;</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span>::<span class="n">chrono</span><span class="p">;</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">fmt</span>::<span class="n">Write</span><span class="p">;</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">A</span>::<span class="p">{</span><span class="bp">self</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">OtherA</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">};</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="bp">self</span>::<span class="n">subsubmodule</span>::<span class="n">C</span><span class="p">;</span><span class="w"></span>
</code></pre></div>



<a name="196713603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196713603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dzmitry Malyshau <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196713603">(May 07 2020 at 00:11)</a>:</h4>
<p>looking further in the <code>submodule</code> it has only items for <code>use</code> statements</p>



<a name="196714712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196714712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196714712">(May 07 2020 at 00:26)</a>:</h4>
<p>iirc mod statements are "inlined" during expansion</p>



<a name="196714728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196714728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196714728">(May 07 2020 at 00:26)</a>:</h4>
<p>so you won't actually parse the submodule (or the subsubmodule) until expansion runs</p>



<a name="196716566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196716566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dzmitry Malyshau <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196716566">(May 07 2020 at 00:57)</a>:</h4>
<p>how do I run the expansion pass?</p>



<a name="196719945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196719945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196719945">(May 07 2020 at 02:03)</a>:</h4>
<p><a href="https://rustc-dev-guide.rust-lang.org/macro-expansion.html" title="https://rustc-dev-guide.rust-lang.org/macro-expansion.html">https://rustc-dev-guide.rust-lang.org/macro-expansion.html</a> may be helpful but I don't have a quick answer I'm afraid</p>



<a name="196720488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196720488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dzmitry Malyshau <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196720488">(May 07 2020 at 02:14)</a>:</h4>
<p>Wait, this doesn't yet make sense. So I'm telling <code>rustc_parse</code> to parse some crate. It goes through the tokens, and then it sees "mod xx", and then what? Just ignores it? If it's not expected to see nested modules at this stage, why not return an error that there is an unexpected?</p>



<a name="196720668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196720668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196720668">(May 07 2020 at 02:18)</a>:</h4>
<p>it's just stored as "mod xx;"</p>



<a name="196720671"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196720671" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196720671">(May 07 2020 at 02:18)</a>:</h4>
<p>similar to how <code>foo!(bar);</code> would be stored</p>



<a name="196720774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196720774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dzmitry Malyshau <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196720774">(May 07 2020 at 02:20)</a>:</h4>
<p>Where would it be stored? I'm not seeing anything like that in the item tree returned</p>



<a name="196793004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196793004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dzmitry Malyshau <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196793004">(May 07 2020 at 16:05)</a>:</h4>
<p>I think I understand better what's going on here. <span class="user-mention" data-user-id="116122">@simulacrum</span> correct me if that's wrong please - the parse functions get a single string or file on the input, and that's the scope of their parsing. So if I have other modules in other files, it simply doesn't care to look at them.<br>
In this case, I need some higher level rustc function to load the whole file tree of the crate.</p>



<a name="196795041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Using%20rustc_ap_rustc_parse%3A%3Aparse_crate_from_file/near/196795041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Using.20rustc_ap_rustc_parse.3A.3Aparse_crate_from_file.html#196795041">(May 07 2020 at 16:19)</a>:</h4>
<p>yeah that's my understanding at least</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>